<template>
  <view class="godds-item">
    <!-- 左侧的盒子-->
    <view class="goods-item-left">
      <radio :checked="item.goods_state" color="#c00000" v-if="showRadio" @click="radioClickHandler"></radio>
      <image :src="item.goods_small_logo || defaultPic" class="goods-pic"></image>
    </view>
    <!-- 右侧的盒子 -->
    <view class="goods-item-right">
      <!-- 商品名字 -->
      <view class="goods-name">{{item.goods_name}}</view>
      <!-- 商品价格 -->
      <view class="goods-info-box">
        <view class="goods-price">￥{{item.goods_price | tofixed}}</view>
        <uni-number-box v-if="showNum" :min="1" :max="9999" :value="item.goods_count" @change="bindChange"></uni-number-box>
      </view>
    </view>
  </view>
</template>

<script>
  export default {
    name: "my-goods",
    data() {
      return {
        // 默认无法加载图片
        defaultPic: 'https://tse4-mm.cn.bing.net/th/id/OIP-C.6TFDb_de3A8i5hGUqn0EhAAAAA?w=238&h=178&c=7&r=0&o=5&pid=1.7'
      };
    },
    methods:{
      // radio组件的发生改变事件处理函数
      radioClickHandler(){
        this.$emit('radio-change',{
          goods_id:this.item.goods_id,
          goods_state:!this.item.goods_state
        })
      },
      // NumberBox组件的发生改变事件处理函数
      bindChange(e){
        this.$emit('num-change',{
          goods_id:this.item.goods_id,
          goods_count:+e
        })
      }
    },
    filters: {
      tofixed(num) {
        return Number(num).toFixed(2)
      }
    },
    props: {
      item: {
        type: Object,
        default: {}
      },
      // 是否展示图片左侧的 radio
      showRadio:{
        type:Boolean,
        // 默认情况不展示
        default:false
      },
      // 是否展示图片左侧的 NumberBox 组件
      showNum:{
        type:Boolean,
        // 默认情况不展示
        default:false
      }
    }
  }
</script>

<style lang="scss">
  .godds-item {
    display: flex;
    padding: 10px 5px;
    border-bottom: 1px solid #9a9a9a;
    width: 750rpx;
    box-sizing: border-box;
    .goods-item-left {
      display: flex;
      justify-content: space-between;
      align-items: center;
      .goods-pic {
        width: 100px;
        height: 100px;
        display: block;
        margin-right: 5px;
      }
    }

    .goods-item-right {
      display: flex;
      flex: 1;
      flex-direction: column;
      justify-content: space-between;

      .goods-name {
        font-size: 13px;
      }

      .goods-info-box {
        display: flex;
        justify-content: space-between;
        align-items: center;
        .goods-price {
          color: red;
        }
      }
    }
  }
</style>